//
//  ReplyViewController.swift
//  DianShangWen
//
//  Created by 周文轩 on 15/4/14.
//  Copyright (c) 2015年 &#21608;&#25991;&#36713;. All rights reserved.
//

import UIKit
import Argo
import Runes

class ReplyViewController: UIViewController, UIScrollViewDelegate, UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var newButton: UIButton!
    @IBOutlet weak var hotButton: UIButton!
    @IBOutlet weak var noAnserButton: UIButton!
    @IBOutlet weak var newButtomView: UIView!
    @IBOutlet weak var hotButtomView: UIView!
    @IBOutlet weak var noAnserButtomView: UIView!
    
    @IBOutlet weak var replyScrollerView: UIScrollView!
    @IBOutlet weak var viewWidth: NSLayoutConstraint!
    @IBOutlet weak var hotTableViewWidth: NSLayoutConstraint!
    @IBOutlet weak var noAnserTableViewWidth: NSLayoutConstraint!
    
    @IBOutlet weak var newTableView: UITableView!
    @IBOutlet weak var hotTableView: UITableView!
    @IBOutlet weak var noAnserTableView: UITableView!
    
    let loginViewController = LoginViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.CreateScorllerView()
        self.CreateNewTableView()
        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(animated: Bool) {
        if loginViewController.loginBool == false {
            let loginStoryboard: UIStoryboard = UIStoryboard(name: "LoginStoryboard", bundle: nil)
            let initialViewController: UIViewController = loginStoryboard.instantiateViewControllerWithIdentifier("LoginNavigation") as! UIViewController
            self.presentViewController(initialViewController, animated: true, completion: nil)
            loginViewController.loginBool = true
        }
    }
    
    func CreateNewTableView() {
        newTableView.delegate = self
        newTableView.dataSource = self
        hotTableView.delegate = self
        hotTableView.dataSource = self
        noAnserTableView.delegate = self
        noAnserTableView.dataSource = self
    }
    
    func CreateScorllerView() {
        replyScrollerView.delegate = self
        
        viewWidth.constant = UIScreen.mainScreen().bounds.size.width*3
        hotTableViewWidth.constant = UIScreen.mainScreen().bounds.size.width
        noAnserTableViewWidth.constant = UIScreen.mainScreen().bounds.size.width*2
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        switch scrollView.contentOffset.x {
        case 0:
            if scrollView.contentOffset.y >= 0 && scrollView.contentSize.height <= scrollView.contentOffset.y+501 && scrollView.contentOffset.y < self.view.frame.size.height && hotButton.tag == 1 {
                newButtons(nil)
            }
        case self.view.frame.size.width:
            hotButtons(nil)
        case self.view.frame.size.width*2:
            noAnserButtons(nil)
        default:
            println("Not Dragging")
        }
    }
    
    @IBAction func newButtons(sender: AnyObject!) {
        newButton.setTitleColor(ResourceProvider.sharedInstance.redColoer, forState: UIControlState.Normal)
        newButtomView.backgroundColor = ResourceProvider.sharedInstance.redColoer
        hotButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        hotButtomView.backgroundColor = UIColor.whiteColor()
        noAnserButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        noAnserButtomView.backgroundColor = UIColor.whiteColor()
        
        replyScrollerView.contentOffset.x = 0
        hotButton.tag = 0
    }
    
    @IBAction func hotButtons(sender: AnyObject!) {
        newButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        newButtomView.backgroundColor = UIColor.whiteColor()
        hotButton.setTitleColor(ResourceProvider.sharedInstance.redColoer, forState: UIControlState.Normal)
        hotButtomView.backgroundColor = ResourceProvider.sharedInstance.redColoer
        noAnserButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        noAnserButtomView.backgroundColor = UIColor.whiteColor()
        
        replyScrollerView.contentOffset.x = self.view.frame.size.width
        hotButton.tag = 1
    }
    
    @IBAction func noAnserButtons(sender: AnyObject!) {
        newButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        newButtomView.backgroundColor = UIColor.whiteColor()
        hotButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        hotButtomView.backgroundColor = UIColor.whiteColor()
        noAnserButton.setTitleColor(ResourceProvider.sharedInstance.redColoer, forState: UIControlState.Normal)
        noAnserButtomView.backgroundColor = ResourceProvider.sharedInstance.redColoer
        
        replyScrollerView.contentOffset.x = self.view.frame.size.width*2
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 9
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("replyTableViewCell", forIndexPath: indexPath) as! ReplyNewTableViewCell
        
        cell.mainTitle.text = "问题问题问题问题问题问题问题问题问题问题问题问题问题"
        cell.subScript.text = "回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答"
        cell.subUserName.text = ",UI设计师，就职于百度"
        
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
